const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    // 列表
    list: [],
    index: '',

    desrc: '',
    music: '',
    image: '',
    width: '0%',
    end: '00.00',
    start: '00.00',

    buttonstatus: true,


    // 是否收藏
    collect:0
  },
  // 收藏
  select(){
    var that = this
    wx.getStorage({
      key: 'puser',
      success: function(res) {
        console.log(res)
        console.log(that.data.list[that.data.index].yuer_id)
        let setdata = {
          token:res.data.token,
          yuer_id: that.data.list[that.data.index].yuer_id
        }
        wx.request({
          url: app.api + app.pyuer_collect,
          method:'POST',
          data:setdata,
          success(e){
            console.log(e)
            if(e.data.code == 0){
              // 改变收藏状态
              that.setData({
                collect: 1
              })
              wx.showToast({
                title: e.data.msg,
                duration:2000
              })
            }else{
              wx.showToast({
                title: e.data.msg,
                icon:'none',
                duration:2000
              })
            }
          }
        })
      },
      fail(e){
        wx.showToast({
          title: '请先登录账号',
          icon:'none',
          duration:2000,
          success(){
            wx.reLaunch({
              url: '../Adlzc/index',
            })
          }
        })
      }
    })
  },
  // 取消收藏
  selected() {
    var that = this
    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log(res)
        console.log(that.data.list[that.data.index].yuer_id)
        let setdata = {
          token: res.data.token,
          yuer_id: that.data.list[that.data.index].yuer_id
        }
        wx.request({
          url: app.api + app.pyuer_collect_cancel,
          method: 'POST',
          data: setdata,
          success(e) {
            console.log(e)
            if (e.data.code == 0) {
              // 改变收藏状态
              that.setData({
                collect: 0
              })
              wx.showToast({
                title: e.data.msg,
                duration: 2000
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
      fail(e) {
        wx.showToast({
          title: '请先登录账号',
          icon: 'none',
          duration: 2000,
          success() {
            wx.reLaunch({
              url: '../Adlzc/index',
            })
          }
        })
      }
    })
  },

  stop(e) {

    this.innerAudioContext.pause(() => { })
    this.setData({
      buttonstatus: true
    })
  },
  play(e) {
    var that = this
    this.setData({
      buttonstatus: false
    })
    this.innerAudioContext.play(() => {

    })
    setTimeout(() => {
      this.innerAudioContext.onTimeUpdate(() => {
        //console.log(this.innerAudioContext.duration)  //总时长
        //console.log(this.innerAudioContext.currentTime)  //当前播放进度
        let sumtime = this.innerAudioContext.duration;
        let newtime = this.innerAudioContext.currentTime;
        let width = newtime / sumtime * 100 + '%';

        // 总时间
        let sumtimem = parseInt(sumtime / 60);
        let sumtimes = parseInt(sumtime % 60);
        let newtimem = parseInt(newtime / 60);
        let newtimes = parseInt(newtime % 60);

        let end = sumtimem + ':' + sumtimes;
        let start = newtimem + ':' + newtimes
        console.log(end)
        console.log(start)
        that.setData({
          width: width,
          end: end,
          start: start,
        })
      })
    }, 500)

    //  自然播放完后
    this.innerAudioContext.onEnded(() => {
      that.setData({
        width: '0%',
        end: '00.00',
        start: '00.00',

        buttonstatus: true
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options)
    let index = parseInt(options.index)
    that.setData({
      limit: index + 10,
      index: index
    })
    that.getlist();


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.innerAudioContext.pause(() => { })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.innerAudioContext.pause(() => { })
    
    // this.setData({
    //     buttonstatus: true
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getlist: function () {
    var that = this
    wx.showLoading({
      title: '正在加载中',
    })
    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log(res)
        let setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit
        }
        console.log(setdata)
        wx.request({
          url: app.api + app.pyuer,
          method: 'POST',
          data: setdata,
          success(e) {
            console.log(e)
            wx.hideLoading()
            if (e.data.code == 0) {
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list,
                  collect: e.data.data.list[that.data.index].collect
                })
                WxParse.wxParse('article', 'html', that.data.list[that.data.index].descr, that, 5);
                that.music();
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: "none",
                    duration: 2000
                  })
                }
              } else if (e.data.data.list.length) {
                let list = that.data.list
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
                console.log(that.data.list)
              } else {
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }
            } else if (e.data.code == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
      fail(){
        let setdata = {
          page: that.data.page,
          limit: that.data.limit
        }
        console.log(setdata)
        wx.request({
          url: app.api + app.pyuer,
          method: 'POST',
          data: setdata,
          success(e) {
            console.log(e)
            wx.hideLoading()
            if (e.data.code == 0) {
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list
                })
                WxParse.wxParse('article', 'html', that.data.list[that.data.index].descr, that, 5);
                that.music();
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: "none",
                    duration: 2000
                  })
                }
              } else if (e.data.data.list.length) {
                let list = that.data.list
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
                console.log(that.data.list)
              } else {
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }
            } else if (e.data.code == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },



  music: function () {
    
    var that = this
    console.log(that.data.list[that.data.index].music)
    if(that.data.list[that.data.index].music==""){
      return false;
    }
    wx.showLoading({
      title: '正在缓冲',
    })
    if (this.innerAudioContext) {
      this.innerAudioContext.destroy()
    }

    this.innerAudioContext = wx.createInnerAudioContext()
    this.innerAudioContext.autoplay = true
    this.innerAudioContext.src = that.data.list[that.data.index].music
    this.innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    setTimeout(() => {
      this.innerAudioContext.onTimeUpdate(() => {
        console.log(this.innerAudioContext.duration)  //总时长
        console.log(this.innerAudioContext.currentTime)  //当前播放进度
        let sumtime = this.innerAudioContext.duration;
        let newtime = this.innerAudioContext.currentTime
        let width = newtime / sumtime * 100 + '%'
        console.log(width)
        // 总时间
        let sumtimem = parseInt(sumtime / 60);
        let sumtimes = parseInt(sumtime % 60);
        if (sumtimem < 10) {
          sumtimem = "0" + sumtimem
        }
        if (sumtimes < 10) {
          sumtimes = "0" + sumtimes
        }
        let newtimem = parseInt(newtime / 60);
        let newtimes = parseInt(newtime % 60);


        if (newtimem < 10) {
          newtimem = "0" + newtimem
        }
        if (newtimes < 10) {
          newtimes = "0" + newtimes
        }

        let end = sumtimem + ':' + sumtimes;
        let start = newtimem + ':' + newtimes
        console.log(end)
        console.log(start)
        that.setData({
          width: width,
          end: end,
          start: start,
        })
      })
    }, 500)

    this.innerAudioContext.onPlay(() => {
      wx: wx.hideLoading()
      that.setData({
        buttonstatus: false
      })
    })

    //  自然播放完后
    this.innerAudioContext.onEnded(() => {
      that.setData({
        width: '0%',
        end: '00.00',
        start: '00.00',

        buttonstatus: true
      })
    })
  },

  next: function () {
    var that = this
    that.setData({
      index: that.data.index + 1
    })
    WxParse.wxParse('article', 'html', that.data.list[that.data.index].descr, that, 5);
    console.log(that.data.index)
    that.music()
  },
  prev: function () {
    var that = this
    if (that.data.index <= 0) {
      return false;
    }

    that.setData({
      index: that.data.index - 1
    })
    WxParse.wxParse('article', 'html', that.data.list[that.data.index].descr, that, 5);
    console.log(that.data.index)
    that.music()
  }










})

